package com.wgy.day42Jsoup;

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by pc on 2021/1/11 20:58
 * Description: LearningJava
 */
/*Jsoup Xpath查询*/
public class JsoupDemo6 {
    public static void main(String[] args) throws IOException, XpathSyntaxErrorException {
        //1.获取student.xml的path
        // String path = JsoupDemo3.class.getClassLoader().getResource("student.xml").getPath();
        //2.获取document对象
        Document document = Jsoup.parse(new File("student.xml"), "utf-8");
        //3.Xpath对象要导入jar包

        //4.根据document对象,创建JXDocument对象
        JXDocument jxDocument = new JXDocument(document);

        //5.结合xpath语法查询

          //5.1查询所有student标签对象
        List<JXNode> jxNodes = jxDocument.selN("//student");
        for (JXNode jxNode: jxNodes){
                System.out.println(jxNode);

           }

        //5.2查询所有student标签下的name标签
        List<JXNode> jxNodes2 =  jxDocument.selN("//student/name");

        for (JXNode jxNode: jxNodes2){
            System.out.println(jxNode);

        }
        //5.3查询所有student标签下带有id属性的name标签
        List<JXNode> jxNodes3 =  jxDocument.selN("//student/name[@id]");

        for (JXNode jxNode: jxNodes3){
            System.out.println(jxNode);

        }
        //5.4查询所有student标签下带有id属性的name标签,且id属性值为1
        List<JXNode> jxNodes4 =  jxDocument.selN("//student/name[@id='1']");

        for (JXNode jxNode: jxNodes4){
            System.out.println(jxNode);

        }
    }
}
